package main

import (
	"context"
	"fmt"

	"github.com/mark3labs/mcp-go/mcp"
	"github.com/mark3labs/mcp-go/server"
)

func main() {
	fmt.Printf("开始启动...\n")
	// 创建新的 MCP 服务器
	s := server.NewMCPServer(
		"计算小样",
		"1.0.0",
		server.WithResourceCapabilities(true, true),
		server.WithLogging(),
		server.WithRecovery(),
	)

	// 添加计算器工具
	calculatorTool := mcp.NewTool("计算",
		mcp.WithDescription("执行基本算术运算"),
		mcp.WithString("运行",
			mcp.Required(),
			mcp.Description("要执行的作（加、减、乘、除）"),
			mcp.Enum("加", "减", "乘", "除"),
		),
		mcp.WithNumber("x",
			mcp.Required(),
			mcp.Description("第一个数字"),
		),
		mcp.WithNumber("y",
			mcp.Required(),
			mcp.Description("第二个数字"),
		),
	)

	// 添加计算器处理程序
	s.AddTool(calculatorTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
		op := request.Params.Arguments["运算"].(string)
		x := request.Params.Arguments["x"].(float64)
		y := request.Params.Arguments["y"].(float64)

		var result float64
		switch op {
		case "加":
			result = x + y
		case "减":
			result = x - y
		case "乘":
			result = x * y
		case "除":
			if y == 0 {
				return mcp.NewToolResultError("不能被零除"), nil
			}
			result = x / y
		}

		return mcp.NewToolResultText(fmt.Sprintf("%.2f", result)), nil
	})

	// 启动服务器
	if err := server.ServeStdio(s); err != nil {
		fmt.Printf("服务器错误: %v\n", err)
	} else {
		fmt.Printf("服务器停止！\n")
	}
}
